<!DOCTYPE html>
<html lang="en">
    <head>
        <meta charset="UTF-8">
        <meta name="viewport" content="width=device-width, initial-scale=1" />
        <title>分单支出</title>
        <% include("/common/common_css_js.html"){} %>
    </head>
    <body>
        <div style="padding-bottom: 5px;">
            <div id="queryForm" class="uc-toolbaroverflow" _borderStyle="border-top:0;">
                <span>分单号：</span><input id="keyBillCode" name="keyBillCode" emptyText="请输入分单号"  class="mini-textbox" selectOnFocus="true" required="false" requiredErrorText="分单号不能为空" />
                <span class="separator"></span>
                <span>运输种类:</span><input id="keyTransportSpeciesId" name="keyTransportSpeciesId" class="mini-combobox" style="width:150px;" textField="name" valueField="id" emptyText="请选择..."
                                         required="false" allowInput="false" showNullItem="false" nullItemText="请选择..." requiredErrorText="没有选择运输种类" onvaluechanged="transportSpeciesIdChange" />
                <span class="separator"></span>
                <span>承运方式:</span><input id="keyTransportWayId" name="keyTransportWayId" class="mini-combobox" style="width:150px;" textField="name" valueField="id" emptyText="请选择..."
                                         required="false" allowInput="false" showNullItem="false" nullItemText="请选择..." requiredErrorText="没有选择承运方式"/>

                <span class="separator"></span>
                <span>发货人:</span>
                <div id="keyDeliveryCustomerId" name="keyDeliveryCustomerId" class="mini-autocomplete" style="width:250px;"  popupWidth="1000" textField="name" valueField="id"
                     url="${base}/pages/customer_manage/queryDeliveryCustomerByKey"  required="false" requiredErrorText="发货人不能为空"
                     searchField="key"
                     emptyText="发货人名称">
                    <div property="columns">
                        <div header="ID" field="id" width="30"></div>
                        <div header="客户名称" field="name"></div>
                        <div header="客户简称" field="shortName"></div>
                        <div header="客户电话" field="phone"></div>
                        <div header="客户地址" field="address"></div>
                    </div>
                </div>

                <span class="separator"></span>
                <span>分单归属部门:</span>
                <input id="keyAttributionDepartmentId" name="keyAttributionDepartmentId" class="mini-treeselect" style="width:250px;" textField="name" valueField="id" emptyText="请选择..."
                       expandOnLoad="2"  virtualScroll="true"
                       required="false" allowInput="true" showNullItem="true" nullItemText="请选择..." requiredErrorText="没有选择归属部门"/>
                <div style="margin-top: 5px"></div>
                <span>提货点:</span>
                <div id="keyPickUpPointId" name="keyPickUpPointId" class="mini-autocomplete" style="width:300px;"  popupWidth="500" textField="name" valueField="id"
                     required="false" requiredErrorText="提货点不能为空"
                     url="${base}/pages/basic_data_manage/xt_pick_up_point/queryPickUpPointByCondition?cityId="
                     searchField="key"
                     emptyText="提货点名称">
                    <div property="columns">
                        <div header="提货点名称" field="name" width="150"></div>
                        <div header="提货点电话" field="phone" width="150"></div>
                        <div header="提货点地址" field="address" width="200"></div>
                    </div>
                </div>

                <span class="separator"></span>
                <span>提货方式:</span>
                <input id="keyPickUpGoodsWayId" name="keyPickUpGoodsWayId" class="mini-combobox" style="width:120px;" textField="name" valueField="id" emptyText="请选择..."
                       required="false" allowInput="false" showNullItem="true" nullItemText="请选择..." requiredErrorText="没有选择提货方式"/>

                <span class="separator"></span>
                <span>开单日期：</span>
                <input id="keyOpenBillStartDate" name="keyOpenBillStartDate"  class="mini-datepicker"   format="yyyy-MM-dd"/>--<input id="keyOpenBillEndDate" name="keyOpenBillEndDate" class="mini-datepicker"  format="yyyy-MM-dd"/>

                <span>代理送货费结费状态:</span>
                <input id="keyAgentDeliveryChargeStatusId" name="keyAgentDeliveryChargeStatusId" class="mini-combobox" style="width:120px;" textField="name" valueField="id" emptyText="请选择..."
                       required="false" allowInput="false" showNullItem="true" nullItemText="请选择..." requiredErrorText="没有选择代理送货费结费状态"/>

                <span>业务费结费状态:</span>
                <input id="keyBusinessCostStatusId" name="keyBusinessCostStatusId" class="mini-combobox" style="width:120px;" textField="name" valueField="id" emptyText="请选择..."
                       required="false" allowInput="false" showNullItem="true" nullItemText="请选择..." requiredErrorText="没有选择业务费结费状态"/>

                <div style="margin-top: 5px"></div>
                <% if(so.hasPermission("nativeBillPay:query")){%>
                    <a class="mini-button" iconCls="icon-search" onclick="searchBill()">查询</a>
                <% } %>

                <% if(so.hasPermission("nativeBillPay:queryCndClear")){%>
                    <a class="mini-button" iconCls="icon-new" onclick="clearSearch()">清空查询条件</a>
                <% } %>

                <span class="separator"></span>

                <% if(so.hasPermission("nativeBillPay:clearChargeAgentExpense")){%>
                    <a class="mini-button" iconCls="icon-new" onclick="clearCharge(381)">结清代理送货费</a>
                <% } %>

                <% if(so.hasPermission("nativeBillPay:clearChargeOperatingExpense")){%>
                    <a class="mini-button" iconCls="icon-new" onclick="clearCharge(382)">结清业务费</a>
                <% } %>

                <span class="separator"></span>

                <% if(so.hasPermission("nativeBillPay:download")){%>
                    <#download_grid id="downloadGrid" theGridId="datagrid1" theFileName="分单支出结费表"/>
                <% } %>
            </div>
        </div>

        <div class = "mini-fit">
            <div id="datagrid1" class="mini-datagrid"
                 style="width: 100%; height: 100%;"
                 url="${base}/pages/finance_manage/pay/queryNatvieBillList" idField="id"
                 showPager="true" pageSize="20" sizeList="[10,20,30,50,100]"
                 multiSelect = "true" allowResize="true"
                 showGroupsSummary="true" showSummaryRow="true" summaryPosition="bottom"
                 allowSortColumn = "true"
                 sortField="createTime"
                 sortOrder="desc"
                 ondrawsummarycell="summaryRight"
            >
                <div property="columns">
                    <div type="checkcolumn" ></div>
                    <div field="id" width="140" headerAlign="center" align="center" allowSort="true" renderer="showNativeBillRender">分单号</div>
                    <div field="openBillDate" width="140" headerAlign="center" align="center" allowSort="false" dateFormat="yyyy-MM-dd HH:mm:ss" renderer="ondayRenderer">开单日期</div>
                    <div field="attributionDepartmentId" width="140" headerAlign="center" align="center" allowSort="false" data-options='{cacheName:"department"}' renderer="commonRender">归属部门</div>
                    <div field="agentDeliveryCharge" width="140" headerAlign="center" align="right" allowSort="false" renderer="public_moneyRenderer" summaryType="sum">代理送货费</div>
                    <div field="agentDeliveryChargeStatusId" width="140" headerAlign="center" align="center" allowSort="false" data-options='{cacheName:"sysDic"}' renderer="commonRender">代理送货费状态</div>
                    <div field="serviceCharge" width="140" headerAlign="center" align="right" allowSort="false" renderer="public_moneyRenderer" summaryType="sum">业务费</div>
                    <div field="businessCostStatusId" width="140" headerAlign="center" align="center" allowSort="false" data-options='{cacheName:"sysDic"}' renderer="commonRender">业务费状态</div>
                    <div field="openBillUserId" width="140" headerAlign="center" align="center" allowSort="false" data-options='{cacheName:"sysUser"}' renderer="commonRender">开单人</div>
                    <div field="transportSpeciesId" width="140" headerAlign="center" align="center" allowSort="true" data-options='{cacheName:"sysDic"}' renderer="commonRender">运输种类</div>
                    <div field="transportWayId" width="140" headerAlign="center" align="center" allowSort="true" data-options='{cacheName:"sysDic"}' renderer="commonRender">承运方式</div>
                    <div field="destCityId" width="140" headerAlign="center" align="center" allowSort="true" data-options='{cacheName:"city"}' renderer="commonRender">目的市</div>
                    <div field="deliveryCustomerName" width="140" headerAlign="center" align="center" allowSort="true">发货人名称</div>
                    <div field="deliveryCustomerSalemanId" width="140" headerAlign="center" align="center" allowSort="true" data-options='{cacheName:"sysUser"}' renderer="commonRender">发货客户业务员</div>
                    <div field="pickUpPointName" width="140" headerAlign="center" align="center" allowSort="true">提货点名称</div>
                    <div field="pickUpGoodsWayId" width="140" headerAlign="center" align="center" allowSort="true" data-options='{cacheName:"sysDic"}' renderer="commonRender">提货方式</div>
                    <div field="signChooseId" width="140" headerAlign="center" align="center" allowSort="true" data-options='{cacheName:"sysDic"}' renderer="commonRender">签收方式</div>
                    <div field="airportId" width="140" headerAlign="center" align="center" allowSort="true" data-options='{cacheName:"airport"}' renderer="commonRender">始发机场</div>
                    <div field="flightDate" width="140" headerAlign="center" align="center" allowSort="true" dateFormat="yyyy-MM-dd" renderer="ondayRenderer">班次日期</div>
                    <div field="flightName" width="140" headerAlign="center" align="center" allowSort="true">班次号名称</div>
                    <div field="airlinesId" width="140" headerAlign="center" align="center" allowSort="true" data-options='{cacheName:"airlines"}' renderer="commonRender">航空公司</div>
                    <div field="planTakeOffTime" width="140" headerAlign="center" align="center" allowSort="true">计划出发时间</div>
                    <div field="goodsName" width="140" headerAlign="center" align="center" allowSort="true" >货物名称</div>
                    <div field="volumeInformation" width="140" headerAlign="center" align="center" allowSort="true">体积信息</div>
                    <div field="totalNumber" width="140" headerAlign="center" align="right" allowSort="false" summaryType="sum">总件数</div>
                    <div field="actualWeight" width="140" headerAlign="center" align="right" allowSort="false" renderer="public_moneyRenderer" summaryType="sum">实际重量</div>
                    <div field="chargeableWeight" width="140" headerAlign="center" align="right" allowSort="false" renderer="public_moneyRenderer" summaryType="sum">计费重量</div>
                    <div field="weightRate" width="140" headerAlign="center" align="right" allowSort="false" renderer="public_moneyRenderer">重量费率</div>
                    <div field="volume" width="140" headerAlign="center" align="right" allowSort="false" renderer="public_moneyRenderer" summaryType="sum">体积</div>
                    <div field="volumeRate" width="140" headerAlign="center" align="right" allowSort="false" renderer="public_moneyRenderer">体积费率</div>
                    <div field="transportCost" width="140" headerAlign="center" align="right" allowSort="false" renderer="public_moneyRenderer" summaryType="sum">运费合计</div>
                    <div field="declaredValue" width="140" headerAlign="center" align="right" allowSort="false" renderer="public_moneyRenderer" summaryType="sum">声明价值</div>
                    <div field="insuranceRate" width="140" headerAlign="center" align="right" allowSort="false" renderer="public_moneyRenderer">保费费率</div>
                    <div field="insuranceFee" width="140" headerAlign="center" align="right" allowSort="false" renderer="public_moneyRenderer" summaryType="sum">保费</div>
                    <div field="openBillDeliveryCharge" width="140" headerAlign="center" align="right" allowSort="false" renderer="public_moneyRenderer" summaryType="sum">开单送货费</div>
                    <div field="tax" width="140" headerAlign="center" align="right" allowSort="false" renderer="public_moneyRenderer" summaryType="sum">税金</div>
                    <div field="fuelSurcharge" width="140" headerAlign="center" align="right" allowSort="false" renderer="public_moneyRenderer" summaryType="sum">燃油费</div>
                    <div field="collectionMoney" width="140" headerAlign="center" align="right" allowSort="false" renderer="public_moneyRenderer" summaryType="sum">代收款</div>
                    <div field="otherCharges" width="140" headerAlign="center" align="right" allowSort="false" renderer="public_moneyRenderer" summaryType="sum">其他费用</div>
                    <div field="otherChargesComment" width="140" headerAlign="center" align="center" allowSort="false">其他费用说明</div>
                    <div field="actualTransportCost" width="140" headerAlign="center" align="right" allowSort="false" renderer="public_moneyRenderer" summaryType="sum">实收运费</div>
                    <div field="businessCostAllocationId" width="140" headerAlign="center" align="center" allowSort="false" data-options='{cacheName:"sysDic"}' renderer="commonRender">是否分摊业务费</div>
                    <div field="businessCostAllocationTypeId" width="140" headerAlign="center" align="center" allowSort="false" data-options='{cacheName:"sysDic"}' renderer="commonRender">业务费分摊类型</div>
                    <div field="businessCostComment" width="140" headerAlign="center" align="center" allowSort="false">业务费备注</div>
                </div>
            </div>
        </div>

        <script type="text/javascript">
            mini.parse();

            let keyBillCode = mini.get("keyBillCode");
            let keyTransportSpeciesId = mini.get("keyTransportSpeciesId");
            let keyTransportWayId = mini.get("keyTransportWayId");
            let keyDeliveryCustomerId = mini.get("keyDeliveryCustomerId");
            let keyAttributionDepartmentId = mini.get("keyAttributionDepartmentId");
            let keyPickUpPointId = mini.get("keyPickUpPointId");
            let keyPickUpGoodsWayId = mini.get("keyPickUpGoodsWayId");
            let keyOpenBillStartDate = mini.get("keyOpenBillStartDate");
            let keyOpenBillEndDate = mini.get("keyOpenBillEndDate");
            let keyAgentDeliveryChargeStatusId = mini.get("keyAgentDeliveryChargeStatusId");
            let keyBusinessCostStatusId = mini.get("keyBusinessCostStatusId");

            let grid = mini.get("datagrid1");

            // 定义结清状态
            let settlementStatus = {uncleared:321, cleared:322};

            // 定义费用类型
            let chargeType = {nativeBillAgentDeliveryCharge:381, nativeBillServiceCharge:382, ariMasterBillGroundCharge:390, airMasterBillTotalAmount:391, autoMasterBillTotalAmount:400};

            // 代表的是分单
            let billType = {nativeBill:371, airMasterBill:372, autoMasterBill:373};

            $(function () {
                // 运输种类下拉列表
                let transportSpeciesIdJsonData = commonRenderSysDicDropListByKey("sysDic","XT_DIC_TRANSPORT_SPECIES");
                keyTransportSpeciesId.load(transportSpeciesIdJsonData);

                // 承运方式下拉列表
                let transportWayIdJsonData = commonRenderSysDicDropListByKey("sysDic","XT_DIC_TRANSPORT_WAY");
                keyTransportWayId.load(transportWayIdJsonData);

                // 提货方式下拉列表
                let pickUpGoodsWayIdJsonData = commonRenderSysDicDropListByKey("sysDic","XT_DIC_PICK_UP_GOODS_WAY");
                keyPickUpGoodsWayId.load(pickUpGoodsWayIdJsonData);

                // 部门下拉列表
                let keyAttributionDepartmentIdJsonData = commonRenderDropList("department");
                keyAttributionDepartmentId.loadList(keyAttributionDepartmentIdJsonData,"id","pid");

                // 结费状态
                let keyAgentDeliveryChargeStatusIdJsonData = commonRenderSysDicDropListByKey("sysDic","XT_DIC_SETTLEMENT_STATUS");
                keyAgentDeliveryChargeStatusId.load(keyAgentDeliveryChargeStatusIdJsonData);

                let keyBusinessCostStatusIdJsonData = public_deepClone(keyAgentDeliveryChargeStatusIdJsonData);
                keyBusinessCostStatusId.load(keyBusinessCostStatusIdJsonData);

                // 加载列表数据
                grid.load();
            });

            /**
             * 清空查询条件
             */
            function clearSearch(){
                let form = new mini.Form("#queryForm");
                form.clear();
            }

            /**
             * 根据查询条件查询分单
             */
            function searchBill(){
                let form = new mini.Form("#queryForm");
                let data = form.getData(true);
                grid.load(data);
            }

            //let chargeType = {nativeBillAgentDeliveryCharge:381, nativeBillServiceCharge:382, ariMasterBillGroundCharge:390, airMasterBillTotalAmount:391, autoMasterBillTotalAmount:400};
            /**
             * NATIVE_BILL_AGENT_DELIVERY_CHARGE(381, "分单代理送货费", "分单代理送货费"),
             NATIVE_BILL_SERVICE_CHARGE(382, "分单业务费", "分单业务费"),
             AIR_MASTER_BILL_GROUND_CHARGE(390, "空运主单地面运费", "空运主单地面运费"),
             AIR_MASTER_BILL_TOTAL_AMOUNT(391, "空运主单运费", "空运主单运费"),
             AUTO_MASTER_BILL_TOTAL_AMOUNT(400, "汽运主单运费", "汽运主单运费");
             */
            //let billType = {nativeBill:371, airMasterBill:372, autoMasterBill:373};
            /**
             * 370,0,开单类型,开单类型,XT_DIC_BILL_TYPE
             371,370,分单,分单,
             372,370,航空主单,航空主单,
             373,370,汽运主单,汽运主单,
             */

            /**
             * 结清费用
             */
            function clearCharge(typeId){
                 let selectItems = grid.getSelecteds();
                 if(selectItems.length == 0){
                     showAlert("没有选择需要结清费用的分单行!",null);
                     return;
                 }
                 // 开始遍历
                let clearedBillIds = "";
                let idArray=[];
                for(let billObj of selectItems){
                    let statusId;
                    if(typeId == chargeType.nativeBillAgentDeliveryCharge){
                        statusId = billObj.agentDeliveryChargeStatusId;
                    }
                    else{
                        statusId = billObj.businessCostStatusId;
                    }

                    if(statusId == settlementStatus.cleared){
                        clearedBillIds += billObj.id + " ";
                    }
                    else{
                        let obj;
                        if(typeId == chargeType.nativeBillAgentDeliveryCharge){
                            obj={billId:billObj.id,billAmount:billObj.agentDeliveryCharge};
                        }
                        else{
                            obj={billId:billObj.id,billAmount:billObj.serviceCharge};
                        }
                        idArray.push(obj);
                    }
                }

                if(clearedBillIds.length > 0){
                    if(typeId == chargeType.nativeBillAgentDeliveryCharge){
                        showAlert("分单号【" + clearedBillIds + "】，代理送货费结清状态为【已结清】，请不要选择!");
                    }
                    else{
                        showAlert("分单号【" + clearedBillIds + "】，业务费结清状态为【已结清】，请不要选择!");
                    }
                    return;
                }

                // 到此校验全部成功，可以入库了
                let clearNatvieBillServiceChargeUrl = "${base}/pages/finance_manage/pay/clearedBillCharge";
                let billTypeIdValue = billType.nativeBill;
                let ret = public_getServerData(clearNatvieBillServiceChargeUrl, {idList:idArray,billTypeId:billTypeIdValue,clearChargeTypeId:typeId,statusId:settlementStatus.cleared});
                showAlert(ret.message,function (){
                    grid.reload();
                });

            }


            function showNativeBillRender(e){
                //  renderer="showNativeBillRender"
                let nativeBillId = e.value;
                let htmlStr = "<a href='javascript:void(0);' onclick='showViewNativeBill(\""+ nativeBillId +"\")'>" + nativeBillId + "</a>";
                return htmlStr;
            }

            function showViewNativeBill(nativeBillId){
                let tabUrl="${base}/pages/native_bill_manage/showUpdateBill?opType=view&id=" + nativeBillId;
                parent.window.addTabToMainPage("分单查看","分单查看",tabUrl);
            }

            /**
             * 设置底部统计样式
             * @param e
             */
            function summaryRight(e){
                e.cellStyle="text-align:right;color:red;font-size:18px;font-weight:bold";
            }

        </script>


    </body>
</html>